//
//  JOMessageFrame.m
//  QQ聊天
//
//  Created by song on 15/1/14.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOMessageFrame.h"
#import "JOMessage.h"
@implementation JOMessageFrame

#define JONameFont [UIFont systemFontOfSize:14]
#define MAX(X,Y)  ((X)>(Y)? (X):(Y))

-(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs =@{NSFontAttributeName :font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}


-(void)setMessage:(JOMessage *)message
{
    
    _message = message;
    CGFloat padding = 10;
    CGFloat timeX = 0;
    CGFloat timeY = 0;
    CGFloat timeW = 320;
    CGFloat timeH = 40;
    
    self.timeF = CGRectMake(timeX, timeY, timeW, timeH);
    
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat iconY = CGRectGetMaxY(_timeF);
    CGFloat iconW = 50;
    CGFloat iconH =50;
    CGFloat iconX;
    if(message.type  == JOMessageTypeOther)
    {
        iconX= padding;
    }else{
        iconX = screenW -padding -iconW;
    }
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    
    CGFloat textY = iconY;
    CGFloat textX ;
    CGSize  textSize = [self sizeWithText:message.text font:JONameFont maxSize:CGSizeMake(150, MAXFLOAT)];
    CGSize textBtnSize =CGSizeMake(textSize.width+40, textSize.height+40);
    if(message.type == JOMessageTypeOther)
    {
        textX = CGRectGetMaxX(_iconF) +padding;
    }else{
        textX = iconX -padding -textBtnSize.width ;
    }
    
    _textF = CGRectMake(textX, textY, textSize.width+40, textSize.height+40);
    
    CGFloat textMAXY = CGRectGetMaxY(_textF);
    CGFloat iconMAXY = CGRectGetMaxY(_iconF);
    
    _cellHeight =MAX(textMAXY ,iconMAXY)  +padding;
    
}

@end
